package com.qf.market.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.market.po.Order;
import com.qf.market.po.OrderDetail;
import com.qf.market.service.OrderDetailService;
import com.qf.market.service.OrderService;
import com.qf.market.utils.LayUIOperate;
import com.qf.market.utils.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/orderDetail")
public class OrderDetailController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;

    @RequestMapping("/getOrderDetailList")
    @ResponseBody
    public TableData getOrderDetailList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String orderNum = request.getParameter("orderNum");

        Map params = new HashMap();
        params.put("orderNum", orderNum);

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<OrderDetail> data = orderDetailService.getAllOrderDetailByNum(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/updateOrderDetail")
    @ResponseBody
    public LayUIOperate updateOrderDetail(@RequestBody JSONObject object){
        LayUIOperate operate=new LayUIOperate();
        String orderNum = object.getString("orderNum");
        String totalAmount = object.getString("userPerson");

        int num =  Integer.parseInt(orderNum);
        String orderId = object.getString("orderId");
        String brand = object.getString("brand");
        String type = object.getString("type");
        String model = object.getString("model");
        String amount = object.getString("amount");
        String unit = object.getString("unit");
        String money = object.getString("money");

        Order order = new Order(orderNum,totalAmount);
        OrderDetail orderDetail = new OrderDetail(num,orderId,
                brand,type,model,amount,unit,money,totalAmount);

        boolean f1= orderService.updateOrderTotalAmount(order);
        boolean f2= orderDetailService.updateOrderDetail(orderDetail);
        if(f1&&f2){
            operate.setSuccess(true);
            operate.setMessage("采购单修改成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("采购单修改失败");
        }
        return operate;
    }
}
